import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bsbdj/utils/constant.dart';
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';

import 'bean/story_detail.dart';

class StoriesDetail extends StatefulWidget {
  String _id;
  String _title;

  StoriesDetail(this._id, this._title);

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return StoriesWidget();
  }
}

class StoriesWidget extends State<StoriesDetail> {
  String _detailUrl = "";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getContentUrl();
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return _detailUrl == ""
        ? Scaffold(
            appBar: AppBar(
              title: Text(widget._title),
            ),
            body: Container(
              height: 200,
              alignment: Alignment.center,
              child: Container(
                child: Image.asset(
                  Constant.IMAGE_LOADING,
                  fit: BoxFit.fill,
                ),
              ),
            ),
          )
        : WebviewScaffold(
            appBar: AppBar(
              title: Text(widget._title),
            ),
            url: _detailUrl,
            withJavascript: true,
            withLocalStorage: true,
            withZoom: false,
          );
  }

  void _getContentUrl() async {
    await Dio()
        .get("https://news-at.zhihu.com/api/4/news/${widget._id}")
        .then((value) {
      if (value != null) {
        StoryDetail detail = StoryDetail.fromJson(value.data);
        setState(() {
          _detailUrl = detail.share_url;
        });
      }
    }).catchError((e) {
      print(e.toString());
    });
  }
}
